package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author luoyin
 * @Date 19:46 2022/9/27
 **/
@RestController
@RequestMapping("/api/hosp")
public class ApiScheduleController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/saveSchedule")
    public Result saveSchedule(@RequestParam Map<String,String> paramMap){

        //检验signkey
         if(!checkSign(paramMap)){
             throw new YyghException(201,"校验不匹配");
         }

        scheduleService.save(paramMap);

        return Result.ok();
    }

    @PostMapping("/schedule/list")
    public Result page(@RequestParam Map<String,String> paramMap){
        if(!checkSign(paramMap)){
            throw new YyghException(201,"校验不匹配");
        }
        Page<Schedule> page = scheduleService.findSchedulePage(paramMap);
        return Result.ok(page);
    }

    @PostMapping("/schedule/remove")
    public Result remove(@RequestParam Map<String,String> paramMap){
        if(!checkSign(paramMap)){
            throw new YyghException(201,"校验不成功");
        }
        scheduleService.remove(paramMap);
        return Result.ok();
    }

    public Boolean checkSign(Map<String,String> paramMap){
        //检验signkey
        String sign = paramMap.get("sign");
        String hoscode = paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signMD5 = MD5.encrypt(signKey);
        if(!sign.equals(signMD5)){
            return false;
        }
        return true;
    }
}
